package com.example.demo.controller;

import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import com.example.demo.model.Result;
import com.example.demo.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("/addBook")
    public Result addBook(BookInfo bookInfo) {
        log.info("添加图书，bookInfo：{}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
             return Result.fail("参数错误");
        }
        try {
            bookService.insertBook(bookInfo);
        }catch (Exception e) {
            log.error("添加图书失败，e:{}", e);
            return Result.fail("内部发生错误，请联系管理员");
        }
        return Result.success("");
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest) {
        log.info("获取图书列表，pageRequest:{}", pageRequest);
        if(pageRequest.getCurrentPage() < 1) {
            return Result.fail("非法参数");
        }
        PageResult<BookInfo> pageResult = bookService.queryBookList(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("查询图书信息，bookId:" + bookId);
        if(bookId < 1) {
            log.error("非法图书Id，bookId：" + bookId);
            return null;
        }
        return bookService.queryBookById(bookId);
    }

    /**
     * 更新/删除图书
     * @param bookInfo
     * @return
     */
    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("更新图示， updateBook：{}", bookInfo);
        if(bookInfo.getId() < 0) {
            return Result.fail(false, "id不合法");
        }
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result < 1) {
                return Result.fail(false, "更新失败");
            }
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新图书失败，e:{}", e);
            return Result.fail(false, e.getMessage());
        }
    }

    /**
     * 删除图书
     */
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除数据，ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
            if(result < 1) {
                return false;
            }
            return true;
        }catch (Exception e) {
            log.error("批量删除数据失败，ids:{}, e:{}", ids, e);
            return false;
        }

    }
}
